/*****************************************************
*                                                                          
*               COPYRIGHT (c) 2015-2017 Hargic Corporation                 
*                         All Rights Reserved                              
*                                                                          
* The source code contained or described herein and all documents          
* related to the source code ("Material") are owned by Hargic            
* Corporation or its licensors.  Title to the Material remains             
* with Hargic Corporation or its suppliers and licensors.                  
*                                                                          
* The Material is protected by worldwide copyright and trade secret        
* laws and treaty provisions. No part of the Material may be used,         
* copied, reproduced, modified, published, uploaded, posted, transmitted,  
* distributed, or disclosed in any way except in accordance with the       
* applicable license agreement.                                            
*                                                                          
* No license under any patent, copyright, trade secret or other            
* intellectual property right is granted to or conferred upon you by       
* disclosure or delivery of the Materials, either expressly, by            
* implication, inducement, estoppel, except in accordance with the         
* applicable license agreement.                                            
*                                                                          
* Unless otherwise agreed by Hargic in writing, you may not remove or      
* alter this notice or any other notice embedded in Materials by Hargic    
* or Hargic's suppliers or licensors in any way.                           
*                                                                          
** Author: shengda.huang
** Date: 2018-10-16
*
*****************************************************/

/***** Include files ********************************/
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/shm.h>
#include "syslog.h"

/***** Defines **************************************/
#define SYSLOG_SHM_ID				1234

#define SYSLOG_SHM_BUFF_SIZE		(512)
/***** Define structure *****************************/

/***** Define constant ******************************/

/***** Define global ********************************/
int syslog_shmid;
char *syslog_shared_memory = (void *)0;

/***** Define Prototype of functions ****************/

/**
 * @brief	compute environment initialize
 * @param  None
 * @retval None
 */
int syslog_ce_init(void)
{
	syslog_init();
	memset(syslog_shared_memory, 0x0, SYSLOG_SHM_BUFF_SIZE);
	printf("%s: exit success\n", __func__);
	return 0;
}

/*
 *	process environment initialize
 */
int syslog_init(void)
{
	syslog_shmid = shmget((key_t)SYSLOG_SHM_ID, SYSLOG_SHM_BUFF_SIZE, 0666 | IPC_CREAT);
	if (syslog_shmid == -1)
	{
		fprintf(stderr, "%s: shmget failed\n", __func__);
		return -1;
	}

	syslog_shared_memory = shmat(syslog_shmid, (void *)0, 0);
	if (syslog_shared_memory == (void *)-1)
	{
		fprintf(stderr, "%s: shmat failed\n", __func__);
		return -2;
	}

	printf("%s: memory attached at %X\n", __func__, (int)syslog_shared_memory);
	printf("%s: exit success\n", __func__);
	return 0;
}

int syslog_uninit(void)
{
	if (shmdt(syslog_shared_memory) == -1)
	{
		fprintf(stderr, "%s: shmdt failed\n", __func__);
		return -1;
	}
	printf("%s: exit success\n", __func__);
	return 0;
}

int syslog_ce_uninit(void)
{
	if (shmctl(syslog_shmid, IPC_RMID, 0) == -1)
	{
		fprintf(stderr, "%s: shmctl IPC_RMID failed\n", __func__);
		return -1;
	}
	printf("%s: exit success\n", __func__);
	return 0;
}

int syslog_save_info(unsigned char *pbuf, int len, int offset)
{
	memcpy(syslog_shared_memory + offset, pbuf, len);
	return len;
}

void syslog_dump_mem(unsigned char *str, unsigned int offset, int len)
{
	int i;

	printf("%s:", str);
	for (i=0; i<len; i++)
	{
		if (i % 16 == 0)
			printf("\n%p:", syslog_shared_memory + offset + i);
		printf(" %02x", *(syslog_shared_memory + offset + i));
	}
	printf("\n");
}

/* End of file */



